/*
 * Copyright (c)  2020,  DataPipeline
 * All rights reserved.
 *
 * Id:WebMvcConfig.java   2020-08-20 14:02 wanglijun
 */
package cn.datacare.marmot.config;

import cn.datacare.marmot.util.date.DatePattern;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.base.Charsets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2020
 * </p>
 * <p>
 * Company/Department: DataPipeline
 * </p>
 *
 * @author wanglijun
 * @version 1.0
 * @since 2020-08-20 14:02
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    /***
     * 编码格式指定
     * @return CharacterEncodingFilter
     */
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding(Charsets.UTF_8.name());
        filter.setForceEncoding(true);
        return filter;
    }


    /**
     * Configure the {@link HttpMessageConverter HttpMessageConverters} to use for
     * reading or writing to the body of the parseRequest or parseResponse. If no
     * converters are added, a default list of converters is registered.
     * <p>
     * <strong>Note</strong> that adding converters to the list, turns off default
     * converter registration. To simply add a converter without impacting default
     * registration, consider using the method
     * {@link #extendMessageConverters(List)} instead.
     *
     * @param converters initially an empty list of converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(this.fastJsonHttpMessageConverter());
    }


    /***
     * FastJson转换器
     *
     * @return FastJsonHttpMessageConverter
     */
    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(this.fastJsonConfig());
        return fastJsonHttpMessageConverter;
    }

    /***
     * JSON
     * @return
     */
    @Bean
    public FastJsonConfig fastJsonConfig(){

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // // 自定义时间格式
        fastJsonConfig.setDateFormat(DatePattern.YYYY_MM_DD_HH_MM_SS);
        // WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
        // WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
        // WriteNullNumberAsZero 将数值类型字段的空值输出为0
        // WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false
        // 正常转换 null 值
        fastJsonConfig.setSerializerFeatures( SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.PrettyFormat);
        return fastJsonConfig;
    }


}
